package com.defence.costomapp.activity.statistics;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.defence.costomapp.R;
import com.defence.costomapp.adapter.DingdanAdapter;
import com.defence.costomapp.app.MyApplication;
import com.defence.costomapp.base.BaseActivity;
import com.defence.costomapp.base.Urls;
import com.defence.costomapp.bean.TuikuanListBean;
import com.defence.costomapp.bean.UserInfo;
import com.defence.costomapp.myinterface.RVItemClickListener;
import com.defence.costomapp.utils.AmountUtils;
import com.defence.costomapp.utils.DividerItemDecoration;
import com.defence.costomapp.utils.RefreshUtils.RefreshLayout;
import com.defence.costomapp.utils.SgqUtils;
import com.defence.costomapp.utils.SharePerenceUtil;
import com.defence.costomapp.utils.httputils.HttpInterface;
import com.google.gson.Gson;
import com.just.agentweb.AgentWeb;
import com.just.agentweb.ChromeClientCallbackManager;
import com.loopj.android.http.RequestParams;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TuikuanListActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.liear_left)
    LinearLayout liearLeft;
    @BindView(R.id.middle_title)
    TextView middleTitle;
    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.right_icon)
    ImageView rightIcon;
    @BindView(R.id.liear_right)
    LinearLayout liearRight;
    @BindView(R.id.tuikuanSum)
    TextView tuikuanSum;
    @BindView(R.id.tuikuandingdanshu)
    TextView tuikuandingdanshu;
    @BindView(R.id.tuikuanchengben)
    TextView tuikuanchengben;
    @BindView(R.id.daichuhuoSum)
    TextView daichuhuoSum;
    @BindView(R.id.daichuhuodingdanshu)
    TextView daichuhuodingdanshu;
    @BindView(R.id.daichuhuochengben)
    TextView daichuhuochengben;
    int length = 0;
    String sdate;
    String edate;

    @BindView(R.id.liear_tk1)
    LinearLayout liearTk1;
    @BindView(R.id.liear_tk2)
    LinearLayout liearTk2;
    @BindView(R.id.fab_refundMoney)
    FloatingActionButton fabRefundMoney;
    @BindView(R.id.fl_tk)
    FrameLayout flTk;
    @BindView(R.id.srl)
    PullLoadMoreRecyclerView srl;
    private DingdanAdapter dingdanAdapter;
    private TuikuanListBean tuikuanListBean;
    private String formatid;
    private String machineNumber;
    private String groupMachineNumber;
    private List<TuikuanListBean.ListBean> list;
    private List<TuikuanListBean.ListBean> listdetail;
    private UserInfo userInfo;

    private String topID = "";
    private ChromeClientCallbackManager.ReceivedTitleCallback mReceivedTitleCallback = new ChromeClientCallbackManager.ReceivedTitleCallback() {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            middleTitle.setText(title);
            rightIcon.setVisibility(View.GONE);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tuikuan_list);
        ButterKnife.bind(this);
        sdate = getIntent().getStringExtra("sdate");
        edate = getIntent().getStringExtra("edate");
//        device = getIntent().getStringExtra("device");
        formatid = getIntent().getStringExtra("formatid");
        machineNumber = getIntent().getStringExtra("machineNumber");
        groupMachineNumber = getIntent().getStringExtra("groupMachineNumber");


        middleTitle.setText("退款查询");
        srl.setLinearLayout();
        srl.setFooterViewText("loading");
        srl.getRecyclerView().addItemDecoration(new DividerItemDecoration(
                this, DividerItemDecoration.VERTICAL_LIST));
        srl.setFooterViewTextColor(R.color.blue);
        srl.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                length = 0;
                if (list != null && list.size() > 0) {
                    list.clear();
                }
                if (listdetail != null && listdetail.size() > 0) {
                    listdetail.clear();
                }
                topID = "";
                initdata(length);
            }

            @Override
            public void onLoadMore() {
                length++;
                initdata(length);
            }
        });

        fabRefundMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userInfo = MyApplication.getApp().getUserInfo();
                String groupid = SharePerenceUtil.getStringValueFromSp("groupid");
                AgentWeb.with(TuikuanListActivity.this)//传入Activity
                        .setAgentWebParent(flTk, new LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
                        .useDefaultIndicator()// 使用默认进度条
                        .defaultProgressBarColor() // 使用默认进度条颜色
                        .setReceivedTitleCallback(mReceivedTitleCallback) //设置 Web 页面的 title 回调
                        .createAgentWeb()//
                        .ready()
                        .go(Urls.BaseUrl + "/req-mobile/view/refund_orders.html?" + "phoneAID=" + userInfo.getId() + "&uniqueCode=" + userInfo.getAuthorizationKey() + "&funcType=" + SgqUtils.TONGJI_TYPE + "&adminGroupID=" + groupid + "&_t=" + Math.random() + "&keyA=1652");

                fabRefundMoney.setVisibility(View.GONE);

            }
        });
        initdata(length);
    }

    private void initdata(int length) {
        rightIcon.setImageResource(R.mipmap.all);


        if (TextUtils.isEmpty(formatid) && TextUtils.isEmpty(machineNumber) && TextUtils.isEmpty(groupMachineNumber)) {
            RequestParams params = new RequestParams();
            params.put("length", (length * 10) + "");
            params.put("orderBy", "2");
            params.put("endpag", "10");
            params.put("topID", topID);
            //未选择展示全部
            httpUtils.doPost(Urls.allisttuikuan(), SgqUtils.TONGJI_TYPE, params, new HttpInterface() {

                @Override
                public void onSuccess(Gson gson, Object result, String message) throws JSONException {
                    srl.setPullLoadMoreCompleted();

                    JSONObject jsonObject = new JSONObject(result.toString());
                    tuikuanListBean = gson.fromJson(jsonObject.toString(), TuikuanListBean.class);

                    //                  设置topid
                    topID = tuikuanListBean.getTopID();

                    tuikuanSum.setText("退款总额:" + AmountUtils.changeF2Y(String.valueOf(tuikuanListBean.getTuikuanSum())));
                    tuikuandingdanshu.setText("退款订单数:" + tuikuanListBean.getTuikuandingdanshu() + "");
                    tuikuanchengben.setText("退款成本:" + AmountUtils.changeF2Y(String.valueOf(tuikuanListBean.getTuikuanchengben())));

                    daichuhuoSum.setText("待出货总额:" + AmountUtils.changeF2Y(String.valueOf(tuikuanListBean.getDaichuhuoSum())));
                    daichuhuodingdanshu.setText("待出货订单数:" + tuikuanListBean.getDaichuhuodingdanshu() + "");
                    daichuhuochengben.setText("待出货成本:" + AmountUtils.changeF2Y(String.valueOf(tuikuanListBean.getDaichuhuochengben())));

                    if (list == null)
                        list = new ArrayList();

                    list.addAll(tuikuanListBean.getList());

                    if (dingdanAdapter == null) {
                        dingdanAdapter = new DingdanAdapter(TuikuanListActivity.this, TuikuanListActivity.this.list, new RVItemClickListener() {
                            @Override
                            public void onItemClick(int position) {
                                Intent intent = new Intent(TuikuanListActivity.this, DingdanDetailActivity.class);
                                intent.putExtra("numberID", list.get(position).getNumberID());
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);

                            }
                        });
                        srl.setAdapter(dingdanAdapter);
                    } else {
                        dingdanAdapter.notifyDataSetChanged();
                    }
                }
            });


        } else {

            String groupid = SharePerenceUtil.getStringValueFromSp("groupid");
            RequestParams params = new RequestParams();
            params.put("length", (length * 10) + "");
            params.put("orderBy", "2");
            params.put("endpag", "10");
            params.put("adminGroupID", groupid);
            params.put("sdate", sdate);
            params.put("edate", edate);
            if (!TextUtils.isEmpty(formatid)) {
                params.put("formatid", formatid.replace("'", ""));
            }
            params.put("machineNumber", machineNumber);
            params.put("groupMachineNumber", groupMachineNumber);

            //展示机器
            httpUtils.doPost(Urls.allisttuikuan(), SgqUtils.TONGJI_TYPE, params, new HttpInterface() {

                @Override
                public void onSuccess(Gson gson, Object result, String message) throws JSONException {
                    srl.setPullLoadMoreCompleted();
                    liearTk1.setVisibility(View.GONE);
                    liearTk2.setVisibility(View.GONE);

                    JSONObject jsonObject = new JSONObject(result.toString());
                    tuikuanListBean = gson.fromJson(jsonObject.toString(), TuikuanListBean.class);


                    if (listdetail == null)
                        listdetail = new ArrayList();
                    listdetail.addAll(tuikuanListBean.getList());

                    if (dingdanAdapter == null) {
                        dingdanAdapter = new DingdanAdapter(TuikuanListActivity.this, listdetail, new RVItemClickListener() {
                            @Override
                            public void onItemClick(int position) {
                                Intent intent = new Intent(TuikuanListActivity.this, DingdanDetailActivity.class);
                                intent.putExtra("numberID", listdetail.get(position).getNumberID());
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);

                            }
                        });
                        srl.setAdapter(dingdanAdapter);
                    } else {
                        dingdanAdapter.notifyDataSetChanged();
                    }
                }
            });
        }
    }


    @OnClick({R.id.liear_left, R.id.liear_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.liear_left:
                finish();
                break;
            case R.id.liear_right:
                startActivity(new Intent(TuikuanListActivity.this, TuiKuanSerachActivity.class));
                break;
        }
    }


    public class DingdanAdapter extends RecyclerView.Adapter<DingdanAdapter.ViewHolder> {

        private Context context;
        private LayoutInflater inflater;
        private List<TuikuanListBean.ListBean> list;
        private RVItemClickListener rvItemClickListener;

        public DingdanAdapter(Context context, List<TuikuanListBean.ListBean> list, RVItemClickListener rvItemClickListener) {
            super();
            this.context = context;
            inflater = LayoutInflater.from(context);
            this.list = list;
            this.rvItemClickListener = rvItemClickListener;
        }


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_dingdan, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {


            int payType = list.get(position).getPayType();
            try {
                holder.tv_show.setText(list.get(position).getDescVal());
                if (list.get(position).getTui_val() == 1) {
                    holder.tv_state.setText("手动退款(未丢失)");
                    holder.tv_state.setTextColor(Color.rgb(0, 204, 255));
                } else if (list.get(position).getTui_val() == 2) {
                    holder.tv_state.setText("手动退款(物品丢失)");
                    holder.tv_state.setTextColor(Color.rgb(0, 204, 255));
                } else if (list.get(position).getTui_val() == 3) {
                    holder.tv_state.setText("手动退款(未丢失)");
                    holder.tv_state.setTextColor(Color.rgb(0, 204, 255));
                } else {
                    holder.tv_state.setText("退款成功");
                    holder.tv_state.setTextColor(Color.rgb(255, 204, 0));
                }
                switch ("0") {
                    case "0":
                        holder.tv_payreason.setVisibility(View.GONE);
                        break;
                    case "-1":
                        holder.tv_payreason.setText("出货前落货检测返回错误");
                        break;
                    case "-2":
                        holder.tv_payreason.setText("转动货道失败");
                        break;
                    case "-3":
                        holder.tv_payreason.setText("掉货后落货检测失败");
                        break;
                    case "-4":
                        holder.tv_payreason.setText("未检测到掉货");
                        break;
                    case "-5":
                        holder.tv_payreason.setText("机器硬件故障");
                    case "4":
                        holder.tv_payreason.setVisibility(View.GONE);
                        break;
                    case "10":
                        holder.tv_payreason.setText("订单关闭退款");
                        break;
                    case "11":
                        holder.tv_payreason.setText("支付超时退款");
                        break;
                }

                switch (payType) {
                    case 1:
                        holder.tv_orderFormstate.setText("平台退款订单");
                        holder.tv_orderFormstate.setTextColor(Color.rgb(255, 204, 0));
                        break;
                    case 2:
                        holder.tv_orderFormstate.setText("微信退款订单");
                        holder.tv_orderFormstate.setTextColor(Color.rgb(255, 204, 0));
                        break;
                    case 3:
                        holder.tv_orderFormstate.setText("储值卡退款订单");
                        holder.tv_orderFormstate.setTextColor(Color.rgb(255, 204, 0));
                        break;
                    case 4:
                        holder.tv_orderFormstate.setText("支付宝退款订单");
                        holder.tv_orderFormstate.setTextColor(Color.rgb(255, 204, 0));
                        break;
                    default:
                        break;

                }
                holder.tv_address.setText(list.get(position).getMachinename() + "-" + list.get(position).getItemNo());
                holder.tv_time.setText(list.get(position).getBackTimeline());
                holder.tv_money.setText(AmountUtils.changeF2Y(list.get(position).getPayVal() + ""));
                holder.tv_dannum.setText(list.get(position).getWxTransactionID());
            } catch (Exception e) {
                e.printStackTrace();
            }

            holder.liearitemll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rvItemClickListener.onItemClick(position);
                }
            });

        }

        @Override
        public int getItemCount() {
            return list.size();
        }


        class ViewHolder extends RecyclerView.ViewHolder {

            private TextView tv_show;
            private TextView tv_state;
            private TextView tv_time;
            private TextView tv_money;
            private TextView tv_dannum;
            private TextView tv_address;
            private TextView tv_payreason;
            private TextView tv_orderFormstate;
            private LinearLayout liearitemll;

            public ViewHolder(View itemView) {
                super(itemView);
                tv_show = itemView.findViewById(R.id.tv_show);
                tv_state = itemView.findViewById(R.id.tv_state);
                tv_time = itemView.findViewById(R.id.tv_time);
                tv_money = itemView.findViewById(R.id.tv_money);
                tv_dannum = itemView.findViewById(R.id.tv_dannum);
                liearitemll = itemView.findViewById(R.id.liearitemll);
                tv_address = itemView.findViewById(R.id.tv_ddaddress);
                tv_payreason = itemView.findViewById(R.id.tv_payreason);
                tv_orderFormstate = itemView.findViewById(R.id.tv_orderFormstate);

            }
        }
    }
}
